import sys
import os
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget

class VideoPlayer(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建QMediaPlayer对象
        self.player = QMediaPlayer()
        # 创建QVideoWidget对象
        self.video_widget = QVideoWidget()

        layout = QVBoxLayout()
        layout.addWidget(self.video_widget)

        self.setLayout(layout)
        self.setWindowTitle("视频播放")
        self.resize(1280,720)

        # 将QVideoWidget设置为QMediaPlayer的视频输出
        self.player.setVideoOutput(self.video_widget)

    def play_video(self, video_path):
        # 设置要播放的视频内容
        content = QMediaContent(QUrl.fromLocalFile(video_path))
        self.player.setMedia(content)
        # 播放视频
        self.player.play()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = VideoPlayer()
    # 替换为你的视频文件路径
    video_path = "test.mp4"
    video_path = "/home/liuj/rtsp/234.mp4"
    if len(sys.argv) > 1:
        video_path = sys.argv[1]
    if os.path.exists(video_path):
        player.play_video(video_path)
    else:
        raise("视频文件不存在")
    player.play_video(video_path)
    player.show()
    sys.exit(app.exec_())